package cj.web.uc.controller;

import java.util.EnumSet;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.paoding.rose.web.annotation.Param;
import net.paoding.rose.web.annotation.Path;
import net.paoding.rose.web.annotation.rest.Get;
import net.paoding.rose.web.annotation.rest.Post;
import net.paoding.rose.web.var.Model;
import net.sf.json.JSONObject;

import org.springframework.beans.factory.annotation.Autowired;

import cj.web.constants.MemuEnum;
import cj.web.constants.SexEnum;
import cj.web.uc.service.MemberService;
import cj.web.uc.service.VideoUcService;
import cj.web.yc.utils.Constants;
import cj.web.yc.utils.MD5Security;

/**
 * 用户中心
 * @author Administrator
 *
 */
@Path("uc")
public class MemberController extends BaseController{
	@Autowired
	private MemberService memberService;
	@Autowired
	private VideoUcService videoUcService;
	
	/**
	 * 我得资产
	 * @param model
	 * @param request
	 * @return
	 */
	@Get("infor")
	public String infor(Model model, HttpServletRequest request){
		String memberId = getCookieValueByName(request, "memberId");
		List<Map> memberlis = memberService.getMemberById(Integer.parseInt(memberId));
		if(memberlis!=null && memberlis.size()>0){
			model.add("memberId", memberlis.get(0).get("memberId"));
			model.add("phone", memberlis.get(0).get("phone"));
			model.add("point", memberlis.get(0).get("point"));
		}
		return "/views/uc/infor.vm";
	}
	
	/**
	 * 观看历史page
	 * @param model
	 * @param request
	 * @return
	 */
	@Get("watchHistory")
	public String watchHistory(Model model, HttpServletRequest request){
		model.add("memberId", getCookieValueByName(request, "memberId"));
		return "/views/uc/watchHistory.vm";
	}
	
	/**
	 * 观看历史方法
	 * @param start
	 * @param length
	 * @param request
	 * @return
	 */
	@Post("getWatchHistoryList")
	public String getWatchHistoryList(@Param("start") int start,@Param("length") int length, HttpServletRequest request) {
	
		JSONObject jo = new JSONObject();
		String memberId = getCookieValueByName(request, "memberId");
		
		Map rep = videoUcService.queryWatchHistorylis(Integer.parseInt(memberId), start, length);
		List<Map> objs = (List<Map>) rep.get("watchHistorylis");
		int size = (int) rep.get("size");
		
		jo.put("rows", objs);
		jo.put("total", size);
		return "@"+jo.toString();
	}
	
	
	/**
	 * 消费历史page
	 * @param model
	 * @param request
	 * @return
	 */
	@Get("payHistory")
	public String payHistory(Model model, HttpServletRequest request){
		String memberId = getCookieValueByName(request, "memberId");
		model.add("memberId", getCookieValueByName(request, "memberId"));
		return "/views/uc/payHistory.vm";
	}
	
	/**
	 * 消费历史方法
	 * @param start
	 * @param length
	 * @param request
	 * @return
	 */
	@Post("getPayHistoryList")
	public String getPayHistoryList(@Param("start") int start,@Param("length") int length, HttpServletRequest request) {
	
		JSONObject jo = new JSONObject();
		String memberId = getCookieValueByName(request, "memberId");
		
		Map rep = videoUcService.queryPayHistorylis(Integer.parseInt(memberId), start, length);
		List<Map> objs = (List<Map>) rep.get("payHistorylis");
		int size = (int) rep.get("size");
		
		jo.put("rows", objs);
		jo.put("total", size);
		return "@"+jo.toString();
	}
	
	//---------------------------------------
	/**
	 * 修改密码界面
	 * @param model
	 * @param request
	 * @return
	 */
	@Get("upPwdPage")
	public String upPwdPage(Model model, HttpServletRequest request){
		if(isLogin(request)){
			String phone = getCookieValueByName(request, "phone");
			String memberId = getCookieValueByName(request, "memberId");
			model.add("memberId", memberId);
			model.add("phone", phone);
		}
		model.add("currentTopMemu", MemuEnum.memugrzx);
		return "/views/uc/wx/upPwd.vm";
	}
	
	/**
	 * 修改密码界面-PC
	 * @param model
	 * @param request
	 * @return
	 */
	@Get("upPcPwdPage")
	public String upPcPwdPage(Model model, HttpServletRequest request){
		if(isLogin(request)){
			String phone = getCookieValueByName(request, "phone");
			String memberId = getCookieValueByName(request, "memberId");
			model.add("memberId", memberId);
			model.add("phone", phone);
		}
		model.add("currentMemu", MemuEnum.xgmm);
		model.add("currentTopMemu", MemuEnum.memugrzx);
		return "/views/uc/upPcPwd.vm";
	}
	
	/**
	 * 修改密码
	 * @param phone
	 * @param newPwd
	 * @param oldPwd
	 * @param response
	 * @return
	 * @throws Exception
	 */
	@Post("upPwd")
	public String upPwd(@Param("memberId")int memberId, @Param("newPwd")String newPwd, 
			@Param("oldPwd")String oldPwd, HttpServletResponse response) throws Exception{
		JSONObject jo = new JSONObject();
		jo.put("returnFlag", Constants.returnObj.returnFlag_true);
		List<Map> uc = memberService.getMemberById(memberId);
		if(uc.size() == 0){
			//不存在
			jo.put("returnFlag", Constants.returnObj.returnFlag_flase);
			jo.put("returnCode", Constants.returnObj.returnCode.code_2);
		}else if(uc.size()>0){
			//存在
			String pwd = uc.get(0).get("pwd").toString();//pwd
			
			if(pwd.equals(MD5Security.md5(oldPwd))){
				memberService.updateMemberId(memberId, MD5Security.md5(newPwd));
			}else{
				jo.put("returnFlag", Constants.returnObj.returnFlag_flase);
			}
		}
		
		return "@" + jo.toString();
	}
	
	/**
	 * 用户完善信息界面
	 * @param model
	 * @param request
	 * @return
	 */
	@Get("upInforPage")
	public String ucInforPage(Model model, HttpServletRequest request){
		model.add("sexObj",EnumSet.allOf(SexEnum.class));
		if(isLogin(request)){
			String memberId = getCookieValueByName(request, "memberId");
			model.add("memberId", memberId);
			List<Map> uc = memberService.getMemberById(Integer.parseInt(memberId));
			if(uc.size()>0){
				model.add("obj", uc.get(0));
			}else{
				model.add("obj", null);
			}
		}
		return "/views/uc/wx/updateInfor.vm";
	}
	
	/**
	 * 用户完善信息界面
	 * @param model
	 * @param request
	 * @return
	 */
	@Get("upPcInforPage")
	public String upPcInforPage(Model model, HttpServletRequest request){
		model.add("sexObj",EnumSet.allOf(SexEnum.class));
		if(isLogin(request)){
			String phone = getCookieValueByName(request, "phone");
			String memberId = getCookieValueByName(request, "memberId");
			model.add("memberId", memberId);
			model.add("phone", phone);
			List<Map> uc = memberService.getMemberById(Integer.parseInt(memberId));
			if(uc.size()>0){
				model.add("obj", uc.get(0));
			}else{
				model.add("obj", null);
			}
		}
		model.add("currentMemu", MemuEnum.wszl);
		model.add("currentTopMemu", MemuEnum.memugrzx);
		return "/views/uc/updatePcInfor.vm";
	}
	
	/**
	 * 用户信息修改
	 * @param model
	 * @param memberId
	 * @param name
	 * @param sex
	 * @param age
	 * @param career
	 * @param request
	 * @return
	 */
	@Post("upInfor")
	public String ucInforPage(Model model,@Param("memberId")int memberId,@Param("name")String name,@Param("sex")String sex,
			@Param("age")String age,@Param("career")String career, HttpServletRequest request,HttpServletResponse response){
		JSONObject jo = new JSONObject();
		jo.put("returnFlag", Constants.returnObj.returnFlag_true);
		List<Map> uc = memberService.getMemberById(memberId);
		if(uc.size() == 0){
			//不存在
			jo.put("returnFlag", Constants.returnObj.returnFlag_flase);
		}else if(uc.size()>0){
			//存在
			setCookie(response, "sex", sex);//性别
			memberService.updateMemberId(memberId, name, age, sex, career);
		}
		return "@" + jo.toString();
	}
}